#ifndef PRIVATEUSERSDB_H
#define PRIVATEUSERSDB_H

#include "CoreFarmAPI.h"



//////////////////////////////////////////
//////PrivateUser node and 'methods'//////
//////////////////////////////////////////
typedef struct PrivateUserNode{
	char userName[USERNAME_LENGTH];
	char userPass[PASS_LENGTH];
	int serverID[SERVERID_LENGTH];
	int userType;
	struct PrivateUserNode *next;

} PrivateUserNode;

PrivateUserNode* privateUserNodeConstructor(){
	PrivateUserNode *thisNode;
	int i = 0;
	thisNode = (PrivateUserNode*)malloc(sizeof(PrivateUserNode));
	assertMem(thisNode);

	thisNode->userType = 0;
	thisNode->next = NULL;
	while( i < SERVERID_LENGTH)
		thisNode->serverID[i++] = 0;
	return thisNode;
}
void privateUserNodeSetUserName(PrivateUserNode *thisNode, char *userName){ strcpy(thisNode->userName, userName); }
void privateUserNodeSetUserPass(PrivateUserNode *thisNode, char *userPass){ strcpy(thisNode->userPass, userPass); }
void privateUserNodeSetServerID(PrivateUserNode *thisNode, int *serverID){ 
	int i = 0;
	while( i < SERVERID_LENGTH && serverID[i])
		thisNode->serverID[i++] = serverID[i]; 
}
void privateUserNodeSetUserType(PrivateUserNode *thisNode, int userType){ thisNode->userType = userType; }
void privateUserNodeSetNext(PrivateUserNode *thisNode, PrivateUserNode *next){ thisNode->next = next; }
char* privateUserNodeGetUserName(PrivateUserNode *thisNode){ return thisNode->userName; }
char* privateUserNodeGetUserPass(PrivateUserNode *thisNode){ return thisNode->userPass; }
int* privateUserNodeGetServerID(PrivateUserNode *thisNode){ return thisNode->serverID; }
int privateUserNodeGetUserType(PrivateUserNode *thisNode){ return thisNode->userType; }
PrivateUserNode* privateUserNodeGetNext(PrivateUserNode *thisNode){ return thisNode->next; }

typedef struct PrivateUsersDB{
	PrivateUserNode *head;
	int size;

} PrivateUsersDB;
PrivateUsersDB *privateUsers_DB = NULL;

void privateUsersDBConstructor(){
	privateUsers_DB = (PrivateUsersDB*)malloc(sizeof(PrivateUsersDB));
	assertMem( privateUsers_DB );
	
	privateUsers_DB->head = NULL;
	privateUsers_DB->size = 0;
}

void privateUsersDBSetHead(PrivateUserNode *head){ privateUsers_DB->head = head; }
PrivateUserNode* privateUsersDBGetHead(){ return privateUsers_DB->head; }

int privateUsersDBIsEmpty(){ return (privateUsers_DB->size==0); }

void privateUsersDBPush(char *userName, char *userPass, int *serverID, int userType){
	
	PrivateUserNode *newPrivateUser = privateUserNodeConstructor();
	privateUserNodeSetUserName(newPrivateUser, userName);
	privateUserNodeSetUserPass(newPrivateUser, userPass);
	privateUserNodeSetUserType(newPrivateUser, userType);
	privateUserNodeSetServerID(newPrivateUser, serverID);

	if( !privateUsers_DB )
		privateUsersDBConstructor();

	if( !privateUsersDBIsEmpty() )
		newPrivateUser->next = privateUsers_DB->head;
	privateUsers_DB->head = newPrivateUser;
	privateUsers_DB->size++;
}

void privateUsersPrintAll(){
	PrivateUserNode *currNode=NULL;
	
	if( privateUsers_DB )
		currNode = privateUsers_DB->head;
	while( currNode ){
		fprintf(stdout, "UserName: %s\nUserPass: %s\nType: %d\nFirst Server: %d\n", currNode->userName, currNode->userPass, currNode->userType, currNode->serverID[0]);
		currNode = currNode->next;
	}
}

#endif